﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace ProgramMain.Google
{
    public class GoogleRectangle : ICloneable, IComparable
    {
        public static readonly GoogleRectangle Empty = new GoogleRectangle();

        public GoogleCoordinate LeftTop
        {
            get
            {
                return new GoogleCoordinate(Left, Top, Level);
            }
        }

        public GoogleCoordinate RightBottom
        {
            get
            {
                return new GoogleCoordinate(Right, Bottom, Level);
            }
        }

        public long Left { get; private set; }

        public long Right { get; private set; }

        public long Top { get; private set; }

        public long Bottom { get; private set; }

        public int Level { get; private set; }

        private GoogleRectangle()
        {
            Left = 0;
            Right = 0;
            Top = 0;
            Bottom = 0;
            Level = 0;
        }

        public GoogleRectangle(long Left, long Top, long Right, long Bottom, int Level)
        {
            this.Left = Left;
            this.Top = Top;
            this.Right = Right;
            this.Bottom = Bottom;
            this.Level = Level;
        }

        public GoogleRectangle(GoogleCoordinate pLeftTop, GoogleCoordinate pRightBottom)
        {
            if (pRightBottom.Level != pLeftTop.Level)
            {
                pRightBottom = new GoogleCoordinate((Coordinate)pRightBottom, pLeftTop.Level);
            }
            Left = pLeftTop.X;
            Top = pLeftTop.Y;
            Right = pRightBottom.X;
            Bottom = pRightBottom.Y;
            Level = pLeftTop.Level;
        }

        public GoogleRectangle(CoordinateRectangle coordinateRect, int level)
        {
            GoogleCoordinate pLeftTop = new GoogleCoordinate(coordinateRect.LeftTop, level);
            GoogleCoordinate pRightBottom = new GoogleCoordinate(coordinateRect.RightBottom, level);
            Left = pLeftTop.X;
            Top = pLeftTop.Y;
            Right = pRightBottom.X;
            Bottom = pRightBottom.Y;
            Level = level;
        }

        #region ICloneable Members
        public object Clone()
        {
            return new GoogleRectangle(Left, Top, Right, Bottom, Level);
        }
        #endregion

        #region IComparable Members
        public int CompareTo(Object obj)
        {
            GoogleRectangle rectangle = (GoogleRectangle)obj;
            int res = this.LeftTop.CompareTo(rectangle.LeftTop);
            if (res != 0) return res;
            return this.RightBottom.CompareTo(rectangle.RightBottom);
        }
        #endregion

        public static implicit operator CoordinateRectangle(GoogleRectangle google)
        {
            return new CoordinateRectangle((Coordinate)google.LeftTop, (Coordinate)google.RightBottom);
        }

        //Стартовый и конечный(видимый) блок битмапа по оси X Y
        public Rectangle GetGoogleBlockView()
        {
            return Rectangle.FromLTRB(
                (int)(Left / GoogleBlock.BlockSize),
                (int)(Top / GoogleBlock.BlockSize),
                (int)((Right - GoogleBlock.BlockSize) / GoogleBlock.BlockSize) + 1,
                (int)((Bottom - GoogleBlock.BlockSize) / GoogleBlock.BlockSize) + 1);
        }

        public Rectangle GetScreenRect(GoogleRectangle googleScreenView)
        {
            if (Level != googleScreenView.Level)
            {
                googleScreenView = new GoogleRectangle(
                    new CoordinateRectangle(
                        (Coordinate)googleScreenView.LeftTop,
                        (Coordinate)googleScreenView.RightBottom),
                    Level);
            }

            Point pt1 = LeftTop.GetScreenPoint(googleScreenView);
            Point pt2 = RightBottom.GetScreenPoint(googleScreenView);

            return Rectangle.FromLTRB(pt1.X, pt1.Y, pt2.X, pt2.Y);
        }

        public InterseptResult PointContains(Coordinate point)
        {
            return ((CoordinateRectangle)this).PointContains(point);
        }

        public InterseptResult RectangleContains(CoordinateRectangle rectangle)
        {
            return ((CoordinateRectangle)this).RectangleContains(rectangle);
        }

        public InterseptResult LineContains(CoordinateRectangle line)
        {
            return ((CoordinateRectangle)this).LineContains(line);
        }
    }
}
